//
//  HZGoodsTabViewCell.m
//  app
//
//  Created by 刘华舟 on 15/5/21.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsTabViewCell.h"
#import "HZTableView.h"
#import "HZImageSourceConstant.h"
#import "UIImageView+URL.h"
#import "UIFont+Conveniently.h"


static const NSInteger kGoodsCellMargin = kHZGoodsTabViewCellMargin;
static const NSInteger kGoodsPictureHeight = 160;
static const NSInteger kGoodsTabLabelHeight = 40;

static const NSInteger kGoodsTabLabelWidth = 180;

static const NSInteger kGoodsCellPadding = 24;
static const NSInteger kGoodsCellTagLabelWidth = 60;

@interface HZGoodsTabViewCell()

@property (strong, nonatomic) UIImageView *goodsImgView;
@property (strong, nonatomic) UITextView *goodsTitleView;
@property (strong, nonatomic) UILabel *temaiLabel;
@property (strong, nonatomic) UILabel *goodsPriceLabel;
@property (strong, nonatomic) UILabel *goodsByerCountLabel;
@property (strong, nonatomic) UILabel *tagLabel;

@property (strong, nonatomic) NSMutableArray *tagsArray;

@end

@implementation HZGoodsTabViewCell

- (NSMutableArray *)tagsArray{
    if (_tagsArray == nil) {
        _tagsArray = [NSMutableArray array];
    }
    return _tagsArray;
}

-(void)drawRect:(CGRect)rect
{
    //[self drawSeperatorLineLeft:[self.ubo uw:0] right:[self.ubo uw:0]];
    [self drawHorizonLineLeft:[self.ubo uw:kSizeMallViewMargin] right:[self.ubo uw:0] at:[self.ubo fh:UB_HEIGHT ph:-0.5] color:HZ_MALL_M_SEPARATOR_CELL_LINE];
}

+ (instancetype)cellWithTableView:(HZTableView *)tableView{
    NSString *cellReuseIdentifier = @"HZGoodsTabViewCell";
   
    HZGoodsTabViewCell * cell= (HZGoodsTabViewCell *)[tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
    if (cell == nil) {
        cell = [[HZGoodsTabViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.frame = CGRectOf([self.ufo fx:UB_LEFT fy:UB_TOP], [UBO_SCREEN fw:UB_WIDTH uh:kGoodsPictureHeight + 2*kGoodsCellMargin]);
        
        self.backgroundColor = HZ_CLEAR_COLOR;
        
        //图片
        self.goodsImgView = [[UIImageView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP uy:kGoodsCellMargin],
                                                                        [self.ubo uw:kGoodsPictureHeight uh:kGoodsPictureHeight])];
        self.goodsImgView.backgroundColor = HZ_WHITE_COLOR;
        self.goodsImgView.layer.masksToBounds = YES;
        self.goodsImgView.layer.borderWidth = [self.ubo ph:1];
        self.goodsImgView.layer.borderColor = HZ_MALL_M_SEPARATOR_CELL_LINE.CGColor;
        //self.goodsImgView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.goodsImgView];
        
        //标题
        self.goodsTitleView = [[UITextView alloc] initWithFrame:CGRectOf([self.goodsImgView.ufo fx:UB_RIGHT ux:kGoodsCellPadding fy:UB_TOP],
                                                                       [self.ubo fw:UB_WIDTH uw:-kGoodsCellPadding-2*kSizeMallViewMargin-kGoodsPictureHeight ow:10 uh:2*kGoodsTabLabelHeight -4])];
        self.goodsTitleView.backgroundColor = HZ_CLEAR_COLOR;
        [self.goodsTitleView setTextColor:HZ_MALL_TEXT_BLACK_COLOR];
        self.goodsTitleView.userInteractionEnabled = NO;
        self.goodsTitleView.textAlignment = NSTextAlignmentLeft;
        self.goodsTitleView.contentInset = UIEdgeInsetsMake(-5, -5, 0, 5);
        [self.goodsTitleView setFont:kHZRFont_Content_L(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus)];
        [self addSubview:self.goodsTitleView];
        
        //特卖Label
        self.temaiLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsImgView.ufo fx:UB_RIGHT ux:kGoodsCellPadding fy:UB_HALF],
                                                                  [self.ubo uw:kHZGoodsViewTeMaiBtnWidth uh:kHZGoodsViewTeMaiBtnHeight])];
        self.temaiLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self.temaiLabel setTextColor:HZ_CLEAR_COLOR];
        self.temaiLabel.textAlignment = NSTextAlignmentCenter;
        [self.temaiLabel setFont:kHZRFont_Content_S(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus)];
        self.temaiLabel.layer.masksToBounds = YES;
        self.temaiLabel.layer.cornerRadius = [self.ubo uw:4];
        [self addSubview:self.temaiLabel];
        
        //价格
        self.goodsPriceLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.temaiLabel.ufo fx:UB_RIGHT ux:kGoodsCellPadding fy:UB_TOP],
                                                                       [self.ubo uw:kGoodsTabLabelWidth uh:kGoodsTabLabelHeight])];
        self.goodsPriceLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self.goodsPriceLabel setTextColor:HZ_MALL_MAIN_THEME_ORANGE];
        self.goodsPriceLabel.textAlignment = NSTextAlignmentLeft;
        [self.goodsPriceLabel setFont:kHZRFont_Menu_S(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus)];
        [self addSubview:self.goodsPriceLabel];
        
        //购买人数
        self.goodsByerCountLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.temaiLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                                                       [self.goodsTitleView.ubo fw:UB_WIDTH uh:kGoodsTabLabelHeight])];
        self.goodsByerCountLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self.goodsByerCountLabel setTextColor:HZ_MALL_TEXT_S_LIGHTGRAY_COLOR];
        self.goodsByerCountLabel.textAlignment = NSTextAlignmentLeft;
        [self.goodsByerCountLabel setFont:kHZRFont_Tag_M(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus)];
        [self addSubview:self.goodsByerCountLabel];
        
        //标签
        self.tagLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsByerCountLabel.ufo fx:UB_CENTER ux:-kSizeMallViewMargin fy:UB_TOP oy:-10],
                                                                [self.goodsByerCountLabel.ubo fw:UB_HALF fh:UB_HEIGHT oh:20])];
        self.tagLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self.tagLabel setTextColor:HZ_MALL_TEXT_S_LIGHTGRAY_COLOR];
        self.tagLabel.textAlignment = NSTextAlignmentRight;
        [self.tagLabel setFont:kHZRFont_Tag_M(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus)];
        self.tagLabel.userInteractionEnabled = YES;
        self.tagLabel.backgroundColor = [UIColor blueColor];
        [self addSubview:self.tagLabel];
        self.tagLabel.hidden = YES;
        
    }
    
    
    return self;
}

- (void)refreshViewWithModel:(HZGoodsModel *)model timeoffset:(NSTimeInterval)timeoffset{
    self.model = model;

}

- (void)setModel:(HZGoodsModel *)model{
    _model = model;
    
    //图像的动画特效
    [self.goodsImgView sd_setImageWithURL:[NSURL URLWithString:model.picURL] placeholderImage:nil options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.goodsImgView.alpha = 0.0f;
        [UIView animateWithDuration:0.5f animations:^{
            self.goodsImgView.alpha = 1.0f;
        }];
    }];
    
    self.goodsTitleView.text = model.productTitle;
    
    self.temaiLabel.frame = CGRectOf([self.goodsImgView.ufo fx:UB_RIGHT ux:0 fy:UB_HALF],
                                     [self.ubo uw:0 uh:kHZGoodsViewTeMaiBtnHeight]);
    [self.temaiLabel setTextColor:HZ_CLEAR_COLOR];
    
    
    self.temaiLabel.backgroundColor = HZ_CLEAR_COLOR;
    
    self.temaiLabel.text = @"";
    
    self.goodsPriceLabel.frame = CGRectOf([self.temaiLabel.ufo fx:UB_RIGHT ux:kGoodsCellPadding fy:UB_TOP],
                                          [self.ubo uw:kGoodsTabLabelWidth uh:kGoodsTabLabelHeight]);
    if ([model.tagPrice floatValue] <= 0.01f) {
        NSMutableAttributedString *priceAtrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@", model.salePrice]];
        
        [priceAtrStr addAttributes:@{NSFontAttributeName:kHZRFont_TitleBar_M(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus),NSForegroundColorAttributeName:HZ_RED_COLOR} range:NSMakeRange(0, model.salePrice.length+1)];
        self.goodsPriceLabel.attributedText = priceAtrStr;
    }else{
        NSMutableAttributedString *priceAtrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@" , model.salePrice]];
      
//        [priceAtrStr addAttributes:@{NSFontAttributeName:kHZRFont_Tag_M(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus),NSForegroundColorAttributeName:HZ_MALL_TEXT_S_LIGHTGRAY_COLOR,NSStrikethroughStyleAttributeName:[NSNumber numberWithBool:YES]} range:NSMakeRange(0, model.tagPrice.length+4)];
       
        [priceAtrStr addAttributes:@{NSFontAttributeName:kHZRFont_TitleBar_M(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus),NSForegroundColorAttributeName:HZ_RED_COLOR} range:NSMakeRange(0, model.salePrice.length+1)];
        
        
        self.goodsPriceLabel.attributedText = priceAtrStr;
    }

    [self.goodsPriceLabel sizeToFit];
    
    
    self.goodsByerCountLabel.text = [NSString stringWithFormat:@"已售%@", model.saleNum];

    
    if(model.shopName.length){
        self.tagLabel.hidden = NO;
        self.tagLabel.text = model.shopName;
    }else{
        self.tagLabel.hidden = YES;
        self.tagLabel.text = @"";
    }
    
    for (UILabel *label in self.tagsArray) {
        [label removeFromSuperview];
    }
    [self.tagsArray removeAllObjects];
    
    if (model.promotion.count) {
        NSInteger index = 0;
        for (NSDictionary *dict in model.promotion) {
            NSString *tag = [dict stringForKey:APIKEY_mall_name nilValue:@""];
            if (tag.length) {
                UILabel *tagLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsPriceLabel.ufo fx:UB_RIGHT ux:index*(kGoodsCellTagLabelWidth+kGoodsCellPadding/2)+kGoodsCellPadding/2 fy:UB_TOP uy:5], [self.ubo uw:kGoodsCellTagLabelWidth uh:kGoodsTabLabelHeight - 10])];
                
                if ([tag isEqualToString:@"赠品"]) {
                    
                    tagLabel.backgroundColor = HZ_MALL_MAIN_THEME_M_PURPLE;
                    [tagLabel setTextColor:HZ_WHITE_COLOR];
                    tagLabel.text = @"赠品";
                }
                if ([tag isEqualToString:@"包邮"]) {
                    tagLabel.backgroundColor = HZ_MALL_MAIN_THEME_ORANGE;
                    [tagLabel setTextColor:HZ_WHITE_COLOR];
                    tagLabel.text = @"包邮";
                }
                if([tag isEqualToString:@"折抵"]){
                    tagLabel.backgroundColor = HZ_MALL_MAIN_THEME_RED;
                    [tagLabel setTextColor:HZ_WHITE_COLOR];
                    tagLabel.text = @"折抵";
                }
                
                tagLabel.textAlignment = NSTextAlignmentCenter;
                [tagLabel setFont:JKAppGlobleFontMinTagText];
                tagLabel.layer.masksToBounds = YES;
                tagLabel.layer.cornerRadius = [self.ubo uw:4];
                [self addSubview:tagLabel];
                [self.tagsArray addObject:tagLabel];
                index++;
            }
        }
    }
    
    self.tagLabel.hidden = YES;

    
}





@end
